package edu;


import java.math.BigDecimal;
import java.util.ArrayList;


public class LaboratoryTwo{
	
	static ArrayList<ArrayList<String>> outData = new ArrayList<>();

	static int current = 0;
	static int currentPtr = 0;


	
	public static void main(String[] arg){
		int num[] = {1,2,3,4,5,6,7,8,9,10};
		double P[] = {0.4,0.2,0.1,0.05, 0.05, 0.05, 0.05, 0.04, 0.03, 0.03};
		int inData[] = {3,4,6,2,5,3,8};
		
		LaboratoryTwo.sizeSingl();
		
		
		LaboratoryTwo.outData.add(new ArrayList<String>());
		LaboratoryTwo.outData.get(current).add("");
		
		LaboratoryTwo.F(num.length-8,num, "");
		
		System.out.println("out:");
		int nn=1;
		Integer numSt = 0;
		String extract = new String();
		BigDecimal x = new BigDecimal(1);
		BigDecimal y = new BigDecimal(0);
		String key="";
		
		
		
		for(int i=0; i<LaboratoryTwo.outData.size(); i++)
			for(int j=0; j<LaboratoryTwo.outData.get(i).size(); j++){
				if(outData.get(i).get(j).length()-1==num.length){
					
					
					//System.out.print(nn++ + ": " + outData.get(i).get(j));
					nn++;
					extract = outData.get(i).get(j);
					
					x = new BigDecimal(1);

						for(int k=0, sum=1; k<extract.length();k++){
							//new Integer(0);
							if(extract.charAt(k) == '0'){ continue;}
							if(k+1<extract.length() && extract.charAt(k+1) == '0'){
								numSt = 10;
							}
							else numSt = extract.charAt(k)-'0';
							//System.out.print(" " + P[numSt-1] );
							if(sum==2) x = x.multiply(new BigDecimal(P[numSt-1]) );
							if(sum==3) {x = x.multiply(new BigDecimal(P[numSt-1]) );x = x.multiply(new BigDecimal(P[numSt-1]) );}
							if(sum==4) x = x.multiply(new BigDecimal(P[numSt-1]) );
							if(sum==5) x = x.multiply(new BigDecimal(P[numSt-1]) );
							if(sum==6) x = x.multiply(new BigDecimal(P[numSt-1]) );
							if(sum==8) x = x.multiply(new BigDecimal(P[numSt-1]) );
							sum++;

						}
						
				x = x.setScale(10, BigDecimal.ROUND_DOWN);
				if( y!=x.max(y) ){
					
					key=outData.get(i).get(j);
					System.out.print(nn + ": " + key);
					y=x.max(y);
					
					System.out.print(" | " + key + " "+ y);
					System.out.println("");
				}
				//System.out.print(" | " + key + " "+ y);

				//System.out.println("");

				}
			}
		
	}
	
	static void F(int number, int mas[], String tail){
		
		
	if(number > 0){
				
		for(int i=0; i < mas.length;i++){

			int a[] = {0,0,0,0,0,0,0,0,0,0};
			for(int p=0; p < mas.length; p++)
			a[p]=mas[p];
			
			if(mas[i] != 0) {
			String tm = LaboratoryTwo.outData.get(currentPtr).get(current);
			LaboratoryTwo.outData.get(currentPtr).set(current, tm + (new Integer(mas[i]).toString()));
			
			a[i]=0;
			//System.out.print(mas[i]);
			Integer nn = new Integer(mas[i]);
			F(number-1,a,tail + nn.toString());

			}
		}
			//System.out.println("");
			
			
			LaboratoryTwo.current++;
			if(current == 10000){
				
				for(int j=0; j<LaboratoryTwo.outData.get(currentPtr).size(); j++){
					if(outData.get(currentPtr).get(j).length()-1!=mas.length){
						//System.out.println("("+ outData.get(currentPtr).get(j).length() + "-" + mas.length + "-" + outData.get(currentPtr).get(j) +")");
						outData.get(currentPtr).remove(j);
						current--;
					}
				}
			}
				
			if(current > 10000){
				current=0; 
				currentPtr++;
				LaboratoryTwo.outData.add(new ArrayList<String>());
			}
			//if(current%5000 == 1)System.out.println(currentPtr + ":" +current);
			LaboratoryTwo.outData.get(currentPtr).add("");
			
			String tm = LaboratoryTwo.outData.get(currentPtr).get(current);
			String ttm = "";
			for(int k=0;k < tail.length()-1;k++){
			//System.out.print(tail.charAt(k));
			ttm+=tail.charAt(k);
			}
			LaboratoryTwo.outData.get(currentPtr).set(current, tm+ttm);
			
	}
	
	}

	static void sizeSingl(){
		int K = 3;
		double[] P = {0.8, 0.15, 0.05};
		//int[] e = {3,3,3,2,3};
		//int[] SU = {0,0,0};
		ArrayList<int[]> str = new ArrayList<>();
		int[] str_in = {0,0,0};
		
		System.out.println("K = " + log2(factorial(K)));
		System.out.println("r = " + log2(K));
		System.out.println("P = " + H(P));
		System.out.println("n >= " + log2(factorial(K))/(log2(K)-H(P)));
	
		str.add(str_in);
	}
	
	public static double log2(double f){
		return Math.log(f)/Math.log(2);
	}
	
	public static int factorial(int num){
		return (num == 0) ? 1 : num * factorial(num - 1);
	 }
	
	public static double H(double[] d){
		double sum=.0;
		
		for(int i=0; i<d.length; i++)
			sum += d[i]*log2(d[i]);
		
		return -sum;
	}

}


